package com.zhang.order.controller;

import com.alibaba.fastjson.JSON;
import com.zhang.common.domain.Order;
import com.zhang.common.domain.Product;
import com.zhang.order.service.OrderService;
import com.zhang.order.service.ProductService;
import com.zhang.order.utils.RestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/api/v1/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    private ProductService productService;

    @RequestMapping(
            value = "getOrderByPid/{pid}",
            method = RequestMethod.GET
    )

    /**
     * 使用nacos+feign
     */
    public Order order(@PathVariable("pid") Integer pid){
        log.info("下单商品id为{}",pid);
        Product pro = productService.findByPid(pid);
        log.info("查询到的商品有货，商品详情：{}",JSON.toJSONString(pro));
        Order order =new Order();
        order.setUId(0001);
        order.setUName("boss");
        order.setPId(pro.getPId());
        order.setPName(pro.getPName());
        order.setPPrice(pro.getPPrice());
        order.setNumber(999);
        orderService.createOrder(order);
        return order;
    }


    // /**
    //  * 使用nacos，从nacos调用服务 + ribbon实现负载均衡
    //  */
    // public Order order(@PathVariable("pid") Integer pid){
    //     log.info("下单商品id为{}",pid);
    //     Product pro = restTemplate.getForObject("http://service-product/api/v1/product/getproduct/" + pid, Product.class);
    //     log.info("查询到的商品有货，商品详情：{}",JSON.toJSONString(pro));
    //     Order order =new Order();
    //     order.setUId(0001);
    //     order.setUName("boss");
    //     order.setPId(pro.getPId());
    //     order.setPName(pro.getPName());
    //     order.setPPrice(pro.getPPrice());
    //     order.setNumber(999);
    //     orderService.createOrder(order);
    //     return order;
    // }
    // /**
    //  * 使用nacos，从nacos调用服务
    //  */
    // public Order order(@PathVariable("pid") Integer pid){
    //     log.info("下单商品id为{}",pid);
    //
    //     List<ServiceInstance> instances = discoveryClient.getInstances("shop-product");
    //     ServiceInstance serviceInstance = instances.get(0);
    //     String host = serviceInstance.getHost();
    //     int port = serviceInstance.getPort();
    //     Product pro = restTemplate.getForObject("http://"+host+":"+port+"/api/v1/product/getproduct/" + pid, Product.class);
    //     log.info("查询到的商品有货，商品详情：{}",JSON.toJSONString(pro));
    //     Order order =new Order();
    //     order.setUId(0001);
    //     order.setUName("boss");
    //     order.setPId(pro.getPId());
    //     order.setPName(pro.getPName());
    //     order.setPPrice(pro.getPPrice());
    //     order.setNumber(999);
    //     orderService.createOrder(order);
    //     return order;
    // }



    /**
     * 写死的url
     */
    // public Order order(@PathVariable("pid") Integer pid){
    //     log.info("下单商品id为{}",pid);
    //     Product pro = restTemplate.getForObject("http://localhost:8081/api/v1/product/getproduct/" + pid, Product.class);
    //     log.info("查询到的商品有货，商品详情：{}",JSON.toJSONString(pro));
    //     Order order =new Order();
    //     order.setUId(0001);
    //     order.setUName("boss");
    //     order.setPId(pro.getPId());
    //     order.setPName(pro.getPName());
    //     order.setPPrice(pro.getPPrice());
    //     order.setNumber(999);
    //     orderService.createOrder(order);
    //     return order;
    // }
}
